package com.fancy.application.sys.permission.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fancy.application.common.controller.BaseController;
import com.fancy.application.common.vo.BasePage;
import com.fancy.application.common.vo.Result;
import com.fancy.application.common.vo.SelectVo;
import com.fancy.application.sys.permission.entity.SysPermissionRes;
import com.fancy.application.sys.permission.entity.SysPermissionUrl;
import com.fancy.application.sys.permission.service.ISysPermissionUrlService;
import com.fancy.application.sys.permission.vo.SysPermissionPage;
import com.fancy.application.sys.permission.vo.SysPermissionUrlVo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
* url权限控制主要逻辑如下：
 * 1、每一个url都有一个父类基础的权限，用于快速验证该请求是否属于此权限下，父类一般为GET权限
 * 2、子权限可以自己扩展自己的权限，并且定义请求方法，但是一般get为页面url可以访问，其他为后台访问，get|post|put可以跳转到你没有权限页面，delete只能提示你没有权限操作此数据
 * 3、父类和子类为且的关系，即，如果父类验证不通过，子类就不必再验证，父类验证通过，子类验证不通过，也属于验证失败
 * 4、待定
* @author wison
* 2020-02-25
*/

@RestController
@RequestMapping("/sys/permission/sys_permission_url")
public class SysPermissionUrlController extends BaseController {
    @Resource
    private ISysPermissionUrlService baseService;

    @PostMapping(value = "/list")
    public Result list(@RequestBody SysPermissionPage<SysPermissionUrl> basePage){
        QueryWrapper<SysPermissionUrl> wrapper = basePage.getWrapper();
        wrapper.eq("fd_module_id",basePage.getFdCategoryId());
        IPage<SysPermissionUrl> sysPermissionUrlPage = baseService.page(basePage.getPage(),wrapper);
        return Result.ok(sysPermissionUrlPage);
    }

    @GetMapping(value = "/listAll")
    public Result listAll(@RequestParam(required = false) String fdModuleId,@RequestParam(required = false) String fdId ){
        if (StrUtil.isEmpty(fdModuleId)) {
            fdModuleId = this.baseService.getById(fdId).getFdModuleId();
        }
        List<SysPermissionUrl> urls = baseService.getParentUrls(fdModuleId);
        List<SelectVo> selectVos = baseService.getSelectVoData(urls);
        return Result.ok().addData(selectVos);
    }

    @PutMapping
    public Result save(@RequestBody SysPermissionUrlVo sysPermissionUrlVo){
        SysPermissionUrl sysPermissionUrl = new SysPermissionUrl();
        BeanUtil.copyProperties(sysPermissionUrlVo,sysPermissionUrl,CopyOptions.create().ignoreNullValue());
        baseService.saveOrUpdate(sysPermissionUrl);
        return Result.ok();
    }
    @DeleteMapping
        public Result del(@RequestParam(required = false) String id ){
        baseService.removeById(id);
        return Result.ok();
    }
    @GetMapping
    public Result view(@RequestParam(required = false) String id ){
        SysPermissionUrlVo sysPermissionUrlVo = new SysPermissionUrlVo();
        SysPermissionUrl sysPermissionUrl = baseService.getById(id);
        BeanUtil.copyProperties(sysPermissionUrl,sysPermissionUrlVo,CopyOptions.create().ignoreNullValue());
        return Result.ok().addData(sysPermissionUrlVo);
    }
}
